﻿/**************************************************************
 *
 * 命名空间：Sgr.UPMS.Application.Validations.Menus
 * 创建时间：2025/3/20
 * 描述：修改菜单命令验证器
 *
 **************************************************************/

using FluentValidation;
using Sgr.UPMS.Application.Commands.Menus;

namespace Sgr.UPMS.Application.Validations.Menus
{
    /// <summary>
    /// 修改菜单命令验证器
    /// </summary>
    public class ModifyMenuCommandValidator : AbstractValidator<UpdateMenuCommand>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ModifyMenuCommandValidator()
        {
            // 菜单ID验证
            RuleFor(x => x.Id)
                .NotEmpty().WithMessage("菜单ID不能为空")
                .GreaterThan(0).WithMessage("菜单ID必须大于0");

            // 菜单名称验证
            RuleFor(x => x.Name)
                .NotEmpty().WithMessage("菜单名称不能为空")
                .MaximumLength(255).WithMessage("菜单名称长度不能超过255个字符");

            // 父级菜单验证
            When(x => x.ParentId.HasValue, () =>
            {
                RuleFor(x => x.ParentId!.Value)
                    .GreaterThanOrEqualTo(0).WithMessage("父级菜单ID不能为负数")
                    .Must((command, parentId) => parentId != command.Id)
                        .WithMessage("父级菜单不能是自己");
            });

            // 图标验证
            When(x => !string.IsNullOrEmpty(x.Icon), () =>
            {
                RuleFor(x => x.Icon)
                    .MaximumLength(255).WithMessage("菜单图标长度不能超过255个字符");
            });

            // 路由验证
            When(x => !string.IsNullOrEmpty(x.RouteName), () =>
            {
                RuleFor(x => x.RouteName)
                    .MaximumLength(255).WithMessage("路由名称长度不能超过255个字符");
            });

            When(x => !string.IsNullOrEmpty(x.RoutePath), () =>
            {
                RuleFor(x => x.RoutePath)
                    .MaximumLength(255).WithMessage("路由路径长度不能超过255个字符");

                When(x => x.ExternalLink, () =>
                {
                    RuleFor(x => x.RoutePath)
                        .Must(path => path!.StartsWith("http://") || path.StartsWith("https://"))
                        .WithMessage("外部链接必须以http://或https://开头");
                });
            });

            When(x => !string.IsNullOrEmpty(x.ComponentPath), () =>
            {
                RuleFor(x => x.ComponentPath)
                    .MaximumLength(255).WithMessage("组件路径长度不能超过255个字符");
            });

            When(x => !string.IsNullOrEmpty(x.RedirectPath), () =>
            {
                RuleFor(x => x.RedirectPath)
                    .MaximumLength(255).WithMessage("重定向长度不能超过255个字符");
            });

            // 权限标识验证
            When(x => !string.IsNullOrEmpty(x.Permission), () =>
            {
                RuleFor(x => x.Permission)
                    .MaximumLength(255).WithMessage("权限标识长度不能超过255个字符");
            });

            // 描述验证
            When(x => !string.IsNullOrEmpty(x.Description), () =>
            {
                RuleFor(x => x.Description)
                    .MaximumLength(500).WithMessage("描述信息长度不能超过500个字符");
            });

            // 目标窗口验证
            When(x => x.Target.HasValue, () =>
            {
                RuleFor(x => x.Target)
                    .IsInEnum().WithMessage("无效的目标窗口类型");
            });

            // 排序号验证
            When(x => x.Order.HasValue, () =>
            {
                RuleFor(x => x.Order!.Value)
                    .GreaterThanOrEqualTo(0).WithMessage("排序号不能为负数");
            });

            // 外部链接路径验证
            When(x => x.ExternalLink && string.IsNullOrEmpty(x.RoutePath), () =>
            {
                RuleFor(x => x.RoutePath)
                    .NotEmpty().WithMessage("外部链接时路由路径不能为空");
            });
        }
    }
}